import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/Const/IFinal.dart';
import 'package:flutter_app/Model/Enums.dart';
import 'package:flutter_app/Model/ProductModel.dart';
import 'package:flutter_app/Page/Main/Other/ShowWeb/main.dart';
import 'package:flutter_app/Util/App/AppUtils.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_yfree/YFree.dart';

class MyViews {
  static Widget view_badge(int badge) {
    return badge > 0
        ? Container(
            constraints: BoxConstraints(minWidth: 16),
            decoration: BoxDecoration(color: Colors.redAccent, borderRadius: BorderRadius.circular(99), border: Border.all(color: Colors.white, width: 1)),
            padding: EdgeInsets.fromLTRB(4, 1, 4, 1),
            alignment: Alignment.center,
            child: Text(badge > 99 ? "99+" : badge.toString(), style: TextStyle(fontSize: 10, color: Colors.white)),
          )
        : YEmpty();
  }

  static Widget view_protocol(YPage page) {
    return Expanded(
      child: RichText(
        text: TextSpan(
          style: TextStyle(color: Colors.grey, fontSize: 12),
          children: [
            TextSpan(text: "已阅读并同意以下协议："),
            TextSpan(
              text: "《${YConfig.appName}平台服务协议》",
              style: TextStyle(color: Colors.black),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  // page.yIntent(ShowContentPage("服务协议", IFinal.Jkids.getProtocol));
                  page.yIntent(ShowWebPage("服务协议", IFinal.Urls.protocol));
                },
            ),
          ],
        ),
      ),
    );
  }

  static Widget view_productItems(YPage page, List<ProductListSubModel> productItems) {
    var imgMinSize = (yScreenSize().width - 30) / 2;

    void closeFloat(ProductListSubModel productItem) {
      page.setState(() => productItem.isSelect = false);
    }

    Future<void> doItemOption(int index, int type) async {
      closeFloat(productItems[index]);
      page.yPost(IFinal.Jkids.doTJItemOption, {
        "type": type,
      }, (isSuccess, results, code, message) {
        if (isSuccess) {
          yToast("操作成功，感谢您的反馈");
          page.setState(() => productItems.removeAt(index));
        }
      });
    }

    List<Widget> view_feedback(int index) {
      double padding = IFinal.Others.isProductShowGrid ? 20 : 10;
      double fontSize = IFinal.Others.isProductShowGrid ? 14 : 13;
      return [
        YButton(
          height: 35,
          borderRadius: BorderRadius.circular(99),
          decoration: BoxDecoration(color: Colors.white),
          child: Container(
            padding: EdgeInsets.only(left: padding, right: padding),
            child: Text("商品不感兴趣", style: TextStyle(fontSize: fontSize)),
          ),
          onTap: () => doItemOption(index, 1),
        ),
        YButton(
          height: 35,
          borderRadius: BorderRadius.circular(99),
          decoration: BoxDecoration(color: Colors.white),
          margin: EdgeInsets.all(padding),
          child: Container(
            padding: EdgeInsets.only(left: padding, right: padding),
            child: Text("屏蔽更多同类", style: TextStyle(fontSize: fontSize)),
          ),
          onTap: () => doItemOption(index, 2),
        ),
        YButton(
          height: 35,
          borderRadius: BorderRadius.circular(99),
          decoration: BoxDecoration(color: Colors.white),
          child: Container(
            padding: EdgeInsets.only(left: padding, right: padding),
            child: Text("图片引起不适", style: TextStyle(fontSize: fontSize)),
          ),
          onTap: () => doItemOption(index, 3),
        ),
      ];
    }

    Widget view_item(ProductListSubModel item, int index, Widget child) {
      return Stack(
        children: [
          YButton(
            onTap: () => AppUtils.goIntent(page, Enum_IntentType.GoProductDetail, params: {
              "id": item.id,
            }),
            child: GestureDetector(
              onLongPress: () {
                productItems.forEach((element) => element.isSelect = false);
                page.setState(() => item.isSelect = true);
              },
              behavior: HitTestBehavior.translucent,
              child: child,
            ),
          ),
          Positioned.fill(
            child: YAnimatedSwitcher(
              child: !item.isSelect
                  ? YEmpty()
                  : GestureDetector(
                      onTap: () => closeFloat(item),
                      child: Container(
                        decoration: BoxDecoration(color: Colors.black.withAlpha(150), borderRadius: BorderRadius.circular(5)),
                        child: Column(
                          children: [
                            Expanded(
                              child: IFinal.Others.isProductShowGrid
                                  ? Column(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: view_feedback(index),
                                    )
                                  : Row(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: view_feedback(index),
                                    ),
                            ),
                            YButton(
                              onTap: () {
                                closeFloat(item);
                                AppUtils.goIntent(page, Enum_IntentType.GoProductList_content, params: {
                                  "content": item.likeContent.isEmpty ? item.title : item.likeContent,
                                });
                              },
                              child: Container(
                                padding: EdgeInsets.only(top: 10, bottom: 10),
                                decoration: BoxDecoration(color: Colors.black.withAlpha(150), borderRadius: BorderRadius.only(bottomLeft: Radius.circular(5), bottomRight: Radius.circular(5))),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text("找相似", style: TextStyle(color: Colors.white, fontSize: 16)),
                                    Icon(Icons.chevron_right, color: Colors.white),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
            ),
          ),
        ],
      );
    }

    Widget view_grid() {
      return MasonryGridView.count(
        primary: false,
        shrinkWrap: true,
        padding: EdgeInsets.only(left: 10, top: 10),
        crossAxisCount: 2,
        itemCount: productItems.length,
        itemBuilder: (context, index) {
          var item = productItems[index];
          return Card(
            elevation: 1,
            color: Colors.white,
            margin: EdgeInsets.fromLTRB(0, 0, 10, 10),
            child: view_item(
              item,
              index,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(5), topRight: Radius.circular(5)),
                    child: Container(
                      constraints: BoxConstraints(minHeight: imgMinSize, minWidth: imgMinSize),
                      child: YImage(null, null, item.img, fit: BoxFit.cover),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(5, 5, 5, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(item.title),
                        YSpacer(height: 10),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            Text("￥", style: TextStyle(fontSize: 12, color: YConfig.themeColor), strutStyle: StrutStyle(forceStrutHeight: true)),
                            Text(item.money1, style: TextStyle(fontSize: 20, color: YConfig.themeColor, fontWeight: FontWeight.bold), strutStyle: StrutStyle(forceStrutHeight: true)),
                            Expanded(child: Text(item.money1 == item.money2 ? "" : "-${item.money2}", style: TextStyle(fontSize: 12, color: YConfig.themeColor), strutStyle: StrutStyle(forceStrutHeight: true))),
                            Text("销量:${item.count}", style: TextStyle(fontSize: 12, color: Colors.grey)),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.only(left: 5),
                          child: Text(item.des, style: TextStyle(color: Color.fromARGB(255, 207, 165, 116))),
                        ),
                      ),
                      YButton(
                        width: 30,
                        height: 30,
                        child: Icon(Icons.more_horiz, color: Colors.grey),
                        onTap: () {
                          productItems.forEach((element) => element.isSelect = false);
                          page.setState(() => item.isSelect = true);
                        },
                      ),
                    ],
                  ),
                ],
              ),
            ),
          );
        },
      );
    }

    Widget view_list() {
      var imgSize = yScreenSize().width / 3;
      return ListView.builder(
        padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
        itemCount: productItems.length,
        primary: false,
        shrinkWrap: true,
        itemBuilder: (context, index) {
          var item = productItems[index];
          return Card(
            elevation: 1,
            color: Colors.white,
            margin: EdgeInsets.only(bottom: 10),
            child: view_item(
              item,
              index,
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(5), bottomLeft: Radius.circular(5)),
                    child: Container(
                      child: YImage(imgSize, imgSize, item.img, fit: BoxFit.cover),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      height: imgSize,
                      padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(item.title),
                          YExpanded(),
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text("￥", style: TextStyle(fontSize: 12, color: YConfig.themeColor), strutStyle: StrutStyle(forceStrutHeight: true)),
                              Text(item.money1, style: TextStyle(fontSize: 20, color: YConfig.themeColor, fontWeight: FontWeight.bold), strutStyle: StrutStyle(forceStrutHeight: true)),
                              Expanded(child: Text(item.money1 == item.money2 ? "" : "-${item.money2}", style: TextStyle(fontSize: 12, color: YConfig.themeColor), strutStyle: StrutStyle(forceStrutHeight: true))),
                              Text("销量:${item.count}", style: TextStyle(fontSize: 12, color: Colors.grey)),
                            ],
                          ),
                          Row(
                            children: [
                              Expanded(child: Text(item.des, style: TextStyle(color: Color.fromARGB(255, 207, 165, 116)))),
                              YButton(
                                width: 30,
                                height: 30,
                                child: Icon(Icons.more_horiz, color: Colors.grey),
                                onTap: () {
                                  productItems.forEach((element) => element.isSelect = false);
                                  page.setState(() => item.isSelect = true);
                                },
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      );
    }

    return productItems.isEmpty
        ? YEmpty()
        : IFinal.Others.isProductShowGrid
            ? view_grid()
            : view_list();
  }

  static PickerStyle pickerStyle() {
    return PickerStyle(
      pickerHeight: yScreenSize().height / 3,
      headDecoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))),
      backgroundColor: IFinal.Colors.pageBG,
      commitButton: Padding(
        padding: EdgeInsets.only(right: 20),
        child: Text("确定", style: TextStyle(color: YConfig.themeColor, fontSize: 16)),
      ),
      cancelButton: Padding(
        padding: EdgeInsets.only(left: 20),
        child: Text("取消", style: TextStyle(color: Colors.grey, fontSize: 16)),
      ),
    );
  }
}
